/* HttpCommunicator.java
 * 
 * Alfred Olschnoegger
 * 03.06.2008
 */

package technical.communication;

import java.io.IOException;
import technical.deParser.DeParser;
import technical.deParser.StringDeParser;

import domain.Game;
import domain.GameObject;
import domain.location.Position;

/**
 * TODO description
 * 
 * @date 03.06.2008
 * @author Alfred Olschnoegger
 */

public class HttpCommunicator extends Communicator {
	// serverURL
//	private static String s_ServerUrl = "http://localhost:8080/rabbitBuster_Server/HttpMethodServlet";
	private static String s_ServerUrl = "http://xen1-awfhoffen.hostingcenter.uclv.net:8080/RabbitBuster/HttpMethodServlet";
	private static HttpCommunicator m_Instance = null;

	
	private myHttpConnection m_Conn = null;

	public static HttpCommunicator getInstance() {
		if (m_Instance == null)
			m_Instance = new HttpCommunicator();

		return m_Instance;
	}

	private HttpCommunicator() {
		
	}


	private synchronized String getResponse(String postVals) throws IOException {
		try {
			m_Conn = new myHttpConnection();
			m_Conn.open(s_ServerUrl);
			m_Conn.send(postVals);
			return m_Conn.recieve();
		} finally {
			m_Conn.close();
		}
	}
	
	public void close() throws IOException {
		m_Conn.close();
	}

	public DeParser LogIn(String username, long key, Game game) throws IOException {
		String str = "action=Auth&task=login&username=" + username + "&key=" + key + "&game=" + game.getGameID();
		return new StringDeParser().Deparse(getResponse(str));
	}

	public DeParser Register(String username, Game game) throws IOException {
		String str = "action=Auth&task=register&username=" + username + "&game=" + game.getGameID();
		return new StringDeParser().Deparse(getResponse(str));
	}

	public DeParser getServerInfo() throws IOException {
		String str = "action=Info&task=server";
		return new StringDeParser().Deparse(getResponse(str));
	}
	
	public DeParser getGames() throws IOException {
		String str = "action=Info&task=game";
		return new StringDeParser().Deparse(getResponse(str));
	}

	public DeParser getFieldUpdate(String username, Position pos, Game game, int range) throws IOException {
		String str = "action=FieldUpdate&game=" + game.getGameID() + "&range=" + range + "&x=" + pos.getX() + "&y=" + pos.getY() +"&username=" + username;
		return new StringDeParser().Deparse(getResponse(str));
	}

	public DeParser useObject(String username, Position pos, Game game, GameObject object) throws IOException {
		String str = "action=GameAction&task=use&game=" + game.getGameID() + "&object=" + object.getID() + "&x=" + pos.getX() + "&y=" + pos.getY() +"&username=" + username;
		return new StringDeParser().Deparse(getResponse(str));
	}
	
	public DeParser getHighscore(Game game) throws IOException {
		String str = "action=User&task=mobilescore&game=" + game.getGameID();
		return new StringDeParser().Deparse(getResponse(str));
	}

} // end of HttpCommunicator.java
